package com.caimiao.miao.http.manager;

import com.caimiao.miao.bean.TicketIn;
import com.caimiao.miao.http.HttpUtil;
import com.caimiao.miao.library.database.DBHelperFactory;
import com.caimiao.miao.library.database.table.TicketInDBHelper;
import com.caimiao.miao.http.retorfit.HttpConfig;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.ObservableSource;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

public class TicketInHttp {

    /*入库*/
    public Disposable addTicket() {

        return Observable
                .create((ObservableOnSubscribe<List<TicketIn>>) emitter -> emitter.onNext(DBHelperFactory.getDBHelper(TicketInDBHelper.class).getNotSyncData()))
                .flatMap((Function<List<TicketIn>, ObservableSource<TicketIn>>) ticketIns -> Observable.fromIterable(ticketIns))
                .flatMap((Function<TicketIn, ObservableSource<TicketIn>>) ticket -> HttpUtil.postInTicket(ticket)
                        .map(response -> {
                            ticket.setSync(response.getState() == HttpConfig.STATE_SUCCESS ? 1 : 2);
                            return ticket;
                        }))
                .subscribeOn(Schedulers.io())
                .subscribe(ticket -> DBHelperFactory
                                .getDBHelper(TicketInDBHelper.class)
                                .updateSync(ticket),
                        throwable -> throwable.printStackTrace());


//        return Observable
//                .create(new ObservableOnSubscribe<TicketIn>() {
//                    @Override
//                    public void subscribe(ObservableEmitter<TicketIn> emitter) throws Exception {
//                        mEmitter = emitter;
//                        sendNext(context);
//                    }
//                })
//                .flatMap(new Function<TicketIn, ObservableSource<TicketIn>>() {
//                    @Override
//                    public ObservableSource<TicketIn> apply(TicketIn ticket) throws Exception {
//                        Map<String, Object> params = new HashMap<>();
//                        params.put("dev_code", ticket.getDevice());
//                        params.put("ticket_num", ticket.getCount());
//                        params.put("ticket_type", ticket.getType());
//                        return RetrofitFactory.create()
//                                .postAddTicket(params)
//                                .map(new Function<Response, TicketIn>() {
//                                    @Override
//                                    public TicketIn apply(Response response) throws Exception {
//                                        ticket.setSync(response.getState() == HttpConfig.STATE_SUCCESS ? 1 : 2);
//                                        return ticket;
//                                    }
//                                });
//                    }
//                })
//                .subscribeOn(Schedulers.io())
//                .observeOn(Schedulers.io())
//                .subscribe(new Consumer<TicketIn>() {
//                    @Override
//                    public void accept(TicketIn ticket) throws Exception {
//                        updateUpload(context, ticket);
//                    }
//                }, new Consumer<Throwable>() {
//                    @Override
//                    public void accept(Throwable throwable) throws Exception {
//                        throwable.printStackTrace();
//                        updateToInit(context);
//                    }
//                }, new Action() {
//                    @Override
//                    public void run() throws Exception {
//                        updateToInit(context);
//                    }
//                });
    }


//    private ObservableEmitter<TicketIn> mEmitter;
//
//    private void sendNext(Context context) {
//        if (mEmitter != null) {
//            TicketIn ticket = DBHelperFactory.getTicketInDBHelper(context).next();
//            if (ticket == null)
//                mEmitter.onComplete();
//            else
//                mEmitter.onNext(ticket);
//        }
//    }
//
//    /*更状态sync*/
//    private void updateUpload(Context context, TicketIn ticket) {
//        IDBHelper dbHelper = DBHelperFactory.getTicketInDBHelper(context);
//        ContentValues values = dbHelper.create(ticket);
//        dbHelper.update(values, TicketInTable._ID + " = ?", new String[]{String.valueOf(ticket.getId())});
//    }
//
//    /*错误sync=2改为初值=0*/
//    private void updateToInit(Context context) {
//        IDBHelper dbHelper = DBHelperFactory.getTicketInDBHelper(context);
//        ContentValues values = new ContentValues();
//        values.put(TicketOutTable.ATTR_SYNC, 0);
//        dbHelper.update(values, TicketOutTable.ATTR_SYNC + " = ?", new String[]{"2"});
//    }
}
